package com.huarui.part.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.model.Dictionary;
import com.huarui.dictionary.service.DictionaryService;
import com.huarui.params.Params;
import com.huarui.part.model.MeasurementUnit;
import com.huarui.part.model.PartCategory;
import com.huarui.part.model.StandardPart;
import com.huarui.part.service.PartService;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class StandardPartAction extends BaseAction {

	private static final long serialVersionUID = 2924526709280705961L;

	private StandardPart stdPart;

	private PartService partService;
	
	private DictionaryService dictionaryService;

	private List<StandardPart> stdPartList;
	/**
	 * 计量单位列表
	 */
	private List<MeasurementUnit> unitList;
	/**
	 * 零件一级类别列表
	 */
	private List<PartCategory> categoryLeve1List;
	/**
	 * 零件子类别列表
	 */
	private List<PartCategory> categorySubList;
	/**
	 * 耐用性
	 */
	private List<Dictionary> durabilityList;

	private String queryParam;	
	
	private Map<String, Object> mesMap;
	
	private Integer start;
	
	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public String toAdd() {

		unitList = partService.getAllUnits();
		categoryLeve1List = partService.getCategoryLevelList();
		durabilityList=dictionaryService.getSubDictionaryListByGroup(DictionaryEnum.GROUP_DURABILITY.toString());
		return "input";
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "stdPart.name", key = "stdPart.name.required"),
			@RequiredStringValidator(fieldName = "stdPart.measurementUnit", key = "stdPart.measurementUnit.required"),
			@RequiredStringValidator(fieldName = "stdPart.durability", key = "stdPart.durability.required") })
	// @RequiredStringValidator(fieldName = "stdPart.level1Id", key =
	// "stdPart.partCategory.level1Id")
	public String doAdd() {

		if (stdPart.getPartCategoryId() == 0) {
			stdPart.setPartCategoryId(stdPart.getLevel1Id());
		}
		stdPart.setPyCode("");
		stdPart.setLevel1Name("");
		stdPart.setLevel2Name("");
		partService.createStdPart(stdPart);

		return SUCCESS;
	}

	public String toList() {

		stdPartList=partService.getStandardPartList();

		return "toList";
	}

	public String toModi() {
		unitList = partService.getAllUnits();
		categoryLeve1List = partService.getCategoryLevelList();
		stdPart=partService.getStandardPartById(stdPart.getId());
		return "toModi";
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "stdPart.name", key = "stdPart.name.required"),
			@RequiredStringValidator(fieldName = "stdPart.measurementUnit", key = "stdPart.measurementUnit.required"),
			@RequiredStringValidator(fieldName = "stdPart.durability", key = "stdPart.durability.required") })
	public String doModi() {

		
		
		
		return SUCCESS;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	public List<MeasurementUnit> getUnitList() {
		return unitList;
	}

	public void setUnitList(List<MeasurementUnit> unitList) {
		this.unitList = unitList;
	}

	public List<PartCategory> getCategoryLeve1List() {
		return categoryLeve1List;
	}

	public void setCategoryLeve1List(List<PartCategory> categoryLeve1List) {
		this.categoryLeve1List = categoryLeve1List;
	}

	public List<PartCategory> getCategorySubList() {
		return categorySubList;
	}

	public void setCategorySubList(List<PartCategory> categorySubList) {
		this.categorySubList = categorySubList;
	}

	public StandardPart getStdPart() {
		return stdPart;
	}

	public void setStdPart(StandardPart stdPart) {
		this.stdPart = stdPart;
	}

	public List<StandardPart> getStdPartList() {
		return stdPartList;
	}

	public void setStdPartList(List<StandardPart> stdPartList) {
		this.stdPartList = stdPartList;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public List<Dictionary> getDurabilityList() {
		return durabilityList;
	}

	public void setDurabilityList(List<Dictionary> durabilityList) {
		this.durabilityList = durabilityList;
	}

	/**
	 * 后台查询
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listStandardPart() {
		Map<String, Object> paramsMap = null;
		if(queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = this.partService.getStandardPartListByComplex(paramsMap);
		return SUCCESS;
	}
}
